﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DBUtility;

namespace XQ.SP.DalServices
{
    //文件临时存放
    public partial class BaseTempDataDal : BaseDal
    {

        public bool ExistsWhere(string whereSql)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BaseTempData");
            if (whereSql != "")
            {
                strSql.Append(" where " + whereSql);
            }

            return DbHelperSQL.Exists(strSql.ToString());
        }
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [BaseTempData]");
            strSql.Append(" where  [Id]=@Id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.BigInt)
            };
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(XQ.SP.Model.BaseTempDataItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [BaseTempData](");
            strSql.Append("[UserId],[SysTap],[FilesTap],[ImagesPath],[ImagesDesc],[Status],[Creator],[Created],[Modifier],[Modified]");
            strSql.Append(") values (");
            strSql.Append("@UserId,@SysTap,@FilesTap,@ImagesPath,@ImagesDesc,@Status,@Creator,@Created,@Modifier,@Modified");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@UserId", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@SysTap", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@FilesTap", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@ImagesPath", SqlDbType.NVarChar,2000) ,            
                        new SqlParameter("@ImagesDesc", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@Modified", SqlDbType.DateTime)             
              
            };

            parameters[0].Value = model.UserId;
            parameters[1].Value = model.SysTap;
            parameters[2].Value = model.FilesTap;
            parameters[3].Value = model.ImagesPath;
            parameters[4].Value = model.ImagesDesc;
            parameters[5].Value = model.Status;
            parameters[6].Value = model.Creator;
            parameters[7].Value = model.Created;
            parameters[8].Value = model.Modifier;
            parameters[9].Value = model.Modified;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt64(obj);

            }

        }

        /// <summary>
        ///   批量添加数据
        /// </summary>
        public int AddList(List<XQ.SP.Model.BaseTempDataItem> models)
        {
            List<CommandInfo> commands = new List<CommandInfo>();
            foreach (XQ.SP.Model.BaseTempDataItem item in models)
            {
                CommandInfo command = new CommandInfo();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into [BaseTempData](");
                strSql.Append("[UserId],[SysTap],[FilesTap],[ImagesPath],[ImagesDesc],[Status],[Creator],[Created],[Modifier],[Modified]");
                strSql.Append(") values (");
                strSql.Append("@UserId,@SysTap,@FilesTap,@ImagesPath,@ImagesDesc,@Status,@Creator,@Created,@Modifier,@Modified");
                strSql.Append(") ");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
                                new SqlParameter("@UserId", SqlDbType.BigInt,8) ,            
                                new SqlParameter("@SysTap", SqlDbType.BigInt,8) ,            
                                new SqlParameter("@FilesTap", SqlDbType.BigInt,8) ,            
                                new SqlParameter("@ImagesPath", SqlDbType.NVarChar,2000) ,            
                                new SqlParameter("@ImagesDesc", SqlDbType.NVarChar,100) ,            
                                new SqlParameter("@Status", SqlDbType.Int,4) ,            
                                new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
                                new SqlParameter("@Created", SqlDbType.DateTime) ,            
                                new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
                                new SqlParameter("@Modified", SqlDbType.DateTime)             
	              
                };

                parameters[0].Value = item.UserId;
                parameters[1].Value = item.SysTap;
                parameters[2].Value = item.FilesTap;
                parameters[3].Value = item.ImagesPath;
                parameters[4].Value = item.ImagesDesc;
                parameters[5].Value = item.Status;
                parameters[6].Value = item.Creator;
                parameters[7].Value = item.Created;
                parameters[8].Value = item.Modifier;
                parameters[9].Value = item.Modified;
                command.CommandText = strSql.ToString();
                command.Parameters = parameters;

                commands.Add(command);
            }

            object obj = DbHelperSQL.ExecuteSqlTran(commands);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }


        }


        /// <summary>
        /// 批量更新数据
        /// </summary>
        public int UpdateList(List<XQ.SP.Model.BaseTempDataItem> models)
        {
            List<CommandInfo> commands = new List<CommandInfo>();
            CommandInfo command = new CommandInfo();

            foreach (XQ.SP.Model.BaseTempDataItem item in models)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update [BaseTempData] set ");

                strSql.Append(" [UserId] = @UserId , ");
                strSql.Append(" [SysTap] = @SysTap , ");
                strSql.Append(" [FilesTap] = @FilesTap , ");
                strSql.Append(" [ImagesPath] = @ImagesPath , ");
                strSql.Append(" [ImagesDesc] = @ImagesDesc , ");
                strSql.Append(" [Status] = @Status , ");
                strSql.Append(" [Creator] = @Creator , ");
                strSql.Append(" [Created] = @Created , ");
                strSql.Append(" [Modifier] = @Modifier , ");
                strSql.Append(" [Modified] = @Modified  ");
                strSql.Append(" where [Id]=@Id ");

                SqlParameter[] parameters = {
                                        new SqlParameter("@Id", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@UserId", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@SysTap", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@FilesTap", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@ImagesPath", SqlDbType.NVarChar,2000) ,		            
                                        new SqlParameter("@ImagesDesc", SqlDbType.NVarChar,100) ,		            
                                        new SqlParameter("@Status", SqlDbType.Int,4) ,		            
                                        new SqlParameter("@Creator", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@Created", SqlDbType.DateTime) ,		            
                                        new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,		            
                                        new SqlParameter("@Modified", SqlDbType.DateTime) 		            
		              
                    };

                parameters[0].Value = item.Id;
                parameters[1].Value = item.UserId;
                parameters[2].Value = item.SysTap;
                parameters[3].Value = item.FilesTap;
                parameters[4].Value = item.ImagesPath;
                parameters[5].Value = item.ImagesDesc;
                parameters[6].Value = item.Status;
                parameters[7].Value = item.Creator;
                parameters[8].Value = item.Created;
                parameters[9].Value = item.Modifier;
                parameters[10].Value = item.Modified;
                command.CommandText = strSql.ToString();
                command.Parameters = parameters;
                commands.Add(command);
            }

            int rows = DbHelperSQL.ExecuteSqlTran(commands);
            return rows;

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(XQ.SP.Model.BaseTempDataItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [BaseTempData] set ");

            strSql.Append(" [UserId] = @UserId , ");
            strSql.Append(" [SysTap] = @SysTap , ");
            strSql.Append(" [FilesTap] = @FilesTap , ");
            strSql.Append(" [ImagesPath] = @ImagesPath , ");
            strSql.Append(" [ImagesDesc] = @ImagesDesc , ");
            strSql.Append(" [Status] = @Status , ");
            strSql.Append(" [Creator] = @Creator , ");
            strSql.Append(" [Created] = @Created , ");
            strSql.Append(" [Modifier] = @Modifier , ");
            strSql.Append(" [Modified] = @Modified  ");
            strSql.Append(" where [Id]=@Id  ");

            SqlParameter[] parameters = {
                        new SqlParameter("@Id", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@UserId", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@SysTap", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@FilesTap", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@ImagesPath", SqlDbType.NVarChar,2000) ,            
                        new SqlParameter("@ImagesDesc", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
                        new SqlParameter("@Modified", SqlDbType.DateTime)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.UserId;
            parameters[2].Value = model.SysTap;
            parameters[3].Value = model.FilesTap;
            parameters[4].Value = model.ImagesPath;
            parameters[5].Value = model.ImagesDesc;
            parameters[6].Value = model.Status;
            parameters[7].Value = model.Creator;
            parameters[8].Value = model.Created;
            parameters[9].Value = model.Modifier;
            parameters[10].Value = model.Modified;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BaseTempData ");
            strSql.Append(" where Id=@Id  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.BigInt)
            };
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BaseTempData ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XQ.SP.Model.BaseTempDataItem GetModel(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, UserId, SysTap, FilesTap, ImagesPath, ImagesDesc, Status, Creator, Created, Modifier, Modified  ");
            strSql.Append("  from BaseTempData ");
            strSql.Append(" where Id=@Id  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.BigInt)
            };
            parameters[0].Value = Id;


            XQ.SP.Model.BaseTempDataItem model = new XQ.SP.Model.BaseTempDataItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = long.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = long.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SysTap"].ToString() != "")
                {
                    model.SysTap = long.Parse(ds.Tables[0].Rows[0]["SysTap"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FilesTap"].ToString() != "")
                {
                    model.FilesTap = long.Parse(ds.Tables[0].Rows[0]["FilesTap"].ToString());
                }
                model.ImagesPath = ds.Tables[0].Rows[0]["ImagesPath"].ToString();
                model.ImagesDesc = ds.Tables[0].Rows[0]["ImagesDesc"].ToString();
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = long.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Created"].ToString() != "")
                {
                    model.Created = DateTime.Parse(ds.Tables[0].Rows[0]["Created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modifier"].ToString() != "")
                {
                    model.Modifier = long.Parse(ds.Tables[0].Rows[0]["Modifier"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modified"].ToString() != "")
                {
                    model.Modified = DateTime.Parse(ds.Tables[0].Rows[0]["Modified"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XQ.SP.Model.BaseTempDataItem GetModelWhereStr(string whereStr)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, UserId, SysTap, FilesTap, ImagesPath, ImagesDesc, Status, Creator, Created, Modifier, Modified  ");
            strSql.Append("  from BaseTempData ");
            if (whereStr != "")
            {
                strSql.Append(" where " + whereStr);
            }

            XQ.SP.Model.BaseTempDataItem model = new XQ.SP.Model.BaseTempDataItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = long.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = long.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SysTap"].ToString() != "")
                {
                    model.SysTap = long.Parse(ds.Tables[0].Rows[0]["SysTap"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FilesTap"].ToString() != "")
                {
                    model.FilesTap = long.Parse(ds.Tables[0].Rows[0]["FilesTap"].ToString());
                }
                model.ImagesPath = ds.Tables[0].Rows[0]["ImagesPath"].ToString();
                model.ImagesDesc = ds.Tables[0].Rows[0]["ImagesDesc"].ToString();
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = long.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Created"].ToString() != "")
                {
                    model.Created = DateTime.Parse(ds.Tables[0].Rows[0]["Created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modifier"].ToString() != "")
                {
                    model.Modifier = long.Parse(ds.Tables[0].Rows[0]["Modifier"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modified"].ToString() != "")
                {
                    model.Modified = DateTime.Parse(ds.Tables[0].Rows[0]["Modified"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM BaseTempData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM BaseTempData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

    }
}

